﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Model;
using Kaidou.Persister;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Kaidou.Util;

public partial class master_master_branch_search : System.Web.UI.Page
{
    private int pageSize = int.Parse(Resources.Resource.pagenumber);

    protected void Page_Load(object sender, EventArgs e)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];

        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
        }
        else if ( loginModel.isKumiaiin( ) )
        {
            Response.Redirect("../menu.aspx");
        }

        string area = Resources.Resource.area;
        string[] areaArray = area.Split(',');
        for (int i = 0; i < areaArray.Length; i++)
        {
            ListItem item = new ListItem();
            item.Value = (i + 1) + "";
            item.Text = areaArray[i];
            office_pref_code.Items.Insert(i + 1, item);
        }

        if (!IsPostBack)
        {
            table.Visible = false;
            none.Visible = false;
        }

        if (Session["back"] != null && (int.Parse(Session["back"].ToString()) == 1))
        {

            BranchSearchModel M = (BranchSearchModel)Session["searchMst"];

           
            if (M.Union_code != null)
            {
                union_code.Text = M.Union_code;
            }

            if (M.Office_name != null)
            {
                office_name.Text = M.Office_name;
            }

            if (M.Office_kana != null)
            {
                office_kana.Text = M.Office_kana;
            }

            if (M.Branch_name != null)
            {
                branch_name.Text = M.Branch_name;
            }

            if (M.Branch_kana != null)
            {
                branch_kana.Text = M.Branch_kana;
            }

            if (M.Office_pref_code > 0)
            {
                office_pref_code.Text = M.Office_pref_code.ToString();
            }
           

            page(int.Parse(Session["pageNo"].ToString()), true);

            Session.Remove("back");
        }
        
    }


    private void page(int no, bool back)
    {
        //カプセル化オブジェクト
        BranchSearchModel M = new BranchSearchModel();

        if (!back)
        {
            if (ParameterCheckUtility.isNumber(branch_code.Text))
            {
                M.Branch_code = int.Parse(branch_code.Text);
            }
            else
            {
                M.Branch_code = -1;
            }

            M.Union_code = union_code.Text.Trim().Length > 0 ? union_code.Text.Trim() : null;
            M.Office_name = office_name.Text.Trim().Length > 0 ? office_name.Text.Trim() : null;
            M.Office_kana = office_kana.Text.Trim().Length > 0 ? office_kana.Text.Trim() : null;
            M.Branch_name = branch_name.Text.Trim().Length > 0 ? branch_name.Text.Trim() : null;
            M.Branch_kana = branch_kana.Text.Trim().Length > 0 ? branch_kana.Text.Trim() : null;
            M.Office_pref_code = office_pref_code.Text.Trim().Length > 0 ? short.Parse(office_pref_code.Text) : (short)0;
        }
        else
        {
            M = (BranchSearchModel)Session["searchMst"];

        }

        Session["searchMst"] = M;
        Session["pageNo"] = no;

        MST_Union_BranchPersister dao = new MST_Union_BranchPersister();
        if (no <= 0) no = 1;
        Pager pager = dao.search(M, no, pageSize);
        
        //if (no > pager.Pages) no = pager.Pages;
        //pager = dao.search(M, no, pageSize);
        pageNo.Value = no.ToString();
        pages.Text = "全" + pager.Total + "件";
        ArrayList list = pager.List;

        if (list.Count == 0)
        {
            table.Visible = false;
            none.Visible = true;
        }
        else
        {
            none.Visible = false;
            table.Visible = true;
            //datatable.Rows.Clear();
            if (no == 1)
            {
                prebutton.Visible = false;
            }
            else
            {
                prebutton.Visible = true;
                prebutton.Text = "≪ 前の" + pageSize + "件";

            }

            if (no == pager.Pages)
            {
                nextbutton.Visible = false;

            }
            else
            {
                nextbutton.Visible = true;
                nextbutton.Text = " 次の" + pageSize + "件 ≫";
            }

            string area = Resources.Resource.area;
            string[] areaArray = area.Split(',');
            LoginModel loginModel = (LoginModel)Session["loginModel"];
            foreach (object o in list)
            {
                BranchSearchModel mu = (BranchSearchModel)o;
                TableRow row = new TableRow();

                TableCell td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.HorizontalAlign = HorizontalAlign.Center;
                td.Text = mu.Union_code;
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.HorizontalAlign = HorizontalAlign.Left;
                td.Text = mu.Office_name;
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.HorizontalAlign = HorizontalAlign.Center;
                td.Text = StringUtil.getBranchCodeNumberFormat( mu.Branch_code);
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.HorizontalAlign = HorizontalAlign.Left;
                td.Text = mu.Branch_name;
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.Text = areaArray[mu.Office_pref_code - 1];
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.Text = mu.Office_city;
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.HorizontalAlign = HorizontalAlign.Center;

                if (loginModel.isOperator())
                {
                    td.Text = "<a href='master_branch_detail.aspx?branchcode=" + mu.Branch_code + "&unioncode=" + mu.Union_code + "'>表示</a>";
                }
                else
                {
                    td.Text = "<a href='master_branch_view.aspx?branchcode=" + mu.Branch_code + "&unioncode=" + mu.Union_code + "'>表示</a>";
                }
                
                row.Cells.Add(td);

                datatable.Rows.Add(row);
            }

        }
    }

    protected void pre(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchMst");
        Session.Remove("pageNo");
        page(int.Parse(pageNo.Value) - 1, false);
    }

    protected void next(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchMst");
        Session.Remove("pageNo");
        page(int.Parse(pageNo.Value) + 1, false);
    }

    protected void search(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchMst");
        Session.Remove("pageNo");
        page(1, false);


    }
}
